﻿using AutoMapper;
using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Write.API.Application.Commands.PhysicalexaminationCommand;
using MediatR;
using Org.BouncyCastle.Crypto.Modes.Gcm;

namespace D.Medicalcare.Write.API.Application.Handlers.PhysicalexaminationHarder
{
    public class AddPhysicalexaminationHarder : IRequestHandler<AddPhysicalexaminationCommand, APIResult<int>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRepository<Physicalexamination> repository;
        private readonly ILogger<AddPhysicalexaminationHarder> logger;

        public AddPhysicalexaminationHarder(IMapper mapper, IBaseRepository<Physicalexamination> repository, ILogger<AddPhysicalexaminationHarder> logger)
        {
            this.mapper = mapper;
            this.repository = repository;
            this.logger = logger;
        }

        public async Task<APIResult<int>> Handle(AddPhysicalexaminationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<int> result = new APIResult<int>();

                var model = mapper.Map<Physicalexamination>(request);
                model.IsDeleted = false;
                model.bookingTime = DateTime.Now;
                model.State = "待体检";
                model.Remark = "";
                model.Checktime = null;
                var xi = repository.AddAsync(model).Result;
          
         
                if (xi > 0)
                {
                  
                    result.Code = APIEnum.Success;
                    result.Msg = "添加成功";
                    result.Data = xi;
        
                    logger.LogInformation("添加预约信息成功");
                }
                else
                {
                    result.Code = APIEnum.Failed;
                    result.Msg = "添加失败";
                    result.Data = xi;
                    logger.LogError("添加预约信息失败");

                }
                return await Task.FromResult(result);
            }
            catch (Exception ex)
            {
                logger.LogError("添加预约信息异常");
                throw;
            }


        }
    }
}
